<?php

namespace App\Observers;

use App\Exceptions\ApiException;
use App\Models\ImageCategory;
use App\Models\Image as ImageModel;

class ImageCategoryObverser
{
    public function saving(ImageCategory  $imageCategory)
    {
        $category=ImageCategory::where('name',$imageCategory['name'])->first();
        if ($category){
            throw new ApiException("名称重新");
        }
        return $imageCategory;
    }

    public function deleting(ImageCategory $imageCategory)
    {
        $img=ImageModel::where('category',$imageCategory['id'])->first();
        if($img)
            throw new ApiException("该分类下有图片，无法删除");
    }

}
